package handler

import (
	"github.com/dgrijalva/jwt-go"
	"github.com/gin-gonic/gin"
	jwt2 "github.com/golangblogs/gojwt"
	"goods/api/request"
	"goods/consts"
	"goods/internal/dao"
	"goods/internal/model"
	"time"
)

func TokenGoods(c *gin.Context) {
	token, err := jwt2.NewJWT(consts.JWT_SECRET_KEY).CreateToken(jwt2.CustomClaims{
		ID: c.GetUint("userId"),
		StandardClaims: jwt.StandardClaims{
			ExpiresAt: time.Now().Unix() + 86400,
		},
	})
	if err != nil {
		c.JSON(200, gin.H{
			"code": 500,
			"msg":  "token获取失败",
			"data": err.Error(),
		})
		return
	}
	c.JSON(200, gin.H{
		"code": 200,
		"msg":  "登录成功",
		"data": token,
	})
}
func CreateGoods(c *gin.Context) {
	var req request.CreateGoodsReq
	if err := c.ShouldBind(&req); err != nil {
		c.JSON(200, gin.H{
			"code": 500,
			"msg":  "查询失败",
			"data": err.Error(),
		})
		return
	}
	var good model.Goods
	dao.GetOneByFields(&model.Goods{GoodsName: req.GoodsName}, &good)
	if good.Id != 0 {
		c.JSON(200, gin.H{
			"code": 10001,
			"msg":  "商品已存在",
			"data": nil,
		})
		return
	}
	var user model.Users
	dao.GetOneByFields(&model.Users{Id: req.UserId}, &user)
	if user.Id != req.UserId {
		c.JSON(200, gin.H{
			"code": 10002,
			"msg":  "用户未登录",
			"data": nil,
		})
		return
	}

	goods := model.Goods{
		UserId:      req.UserId,
		GoodsName:   req.GoodsInfo,
		GoodsTitle:  req.GoodsTitle,
		GoodsImages: req.GoodsImages,
		GoodsInfo:   req.GoodsInfo,
		Price:       req.Price,
	}
	if !dao.Create(&goods) {
		c.JSON(200, gin.H{
			"code": 10003,
			"msg":  "商品发布失败",
			"data": nil,
		})
		return
	}
	c.JSON(200, gin.H{
		"code": 200,
		"msg":  "商品发布成功",
		"data": goods,
	})
	return
}

func QueryGoods(c *gin.Context) {
	var req request.QueryGoodsReq
	if err := c.ShouldBind(&req); err != nil {
		c.JSON(200, gin.H{
			"code": 500,
			"msg":  "查询失败",
			"data": err.Error(),
		})
		return
	}
	var good model.Goods
	dao.GetOneByFields(&model.Goods{Id: req.Id}, &good)
	if good.Id == 0 {
		c.JSON(200, gin.H{
			"code": 10001,
			"msg":  "商品不存在",
			"data": nil,
		})
		return
	}
	c.JSON(200, gin.H{
		"code": 200,
		"msg":  "查询成功",
		"data": good,
	})

}

func UpdateGoods(c *gin.Context) {
	var req request.UpdateGoodsReq
	if err := c.ShouldBind(&req); err != nil {
		c.JSON(200, gin.H{
			"code": 500,
			"msg":  "查询失败",
			"data": err.Error(),
		})
		return
	}
	var good model.Goods
	dao.GetOneByFields(&model.Goods{Id: req.Id}, &good)
	if good.Id == 0 {
		c.JSON(200, gin.H{
			"code": 10001,
			"msg":  "商品不存在",
			"data": nil,
		})
		return
	}
	var user model.Users
	dao.GetOneByFields(&model.Users{Id: req.UserId}, &user)
	if user.Id != req.UserId {
		c.JSON(200, gin.H{
			"code": 10002,
			"msg":  "用户未登录",
			"data": nil,
		})
		return
	}
	goods := model.Goods{
		Id:          req.Id,
		UserId:      req.UserId,
		GoodsName:   req.GoodsName,
		GoodsTitle:  req.GoodsTitle,
		GoodsImages: req.GoodsImages,
		GoodsInfo:   req.GoodsInfo,
		Price:       req.Price,
	}
	if !dao.Update(&goods) {
		c.JSON(200, gin.H{
			"code": 10002,
			"msg":  "修改失败",
			"data": nil,
		})
		return
	}
	c.JSON(200, gin.H{
		"code": 200,
		"msg":  "修改成功",
		"data": goods,
	})
}

func DeleteGoods(c *gin.Context) {
	var req request.DeleteGoodsReq
	if err := c.ShouldBind(&req); err != nil {
		c.JSON(200, gin.H{
			"code": 500,
			"msg":  "查询失败",
			"data": err.Error(),
		})
		return
	}
	var good model.Goods
	dao.GetOneByFields(&model.Goods{Id: req.Id}, &good)
	if good.Id == 0 {
		c.JSON(200, gin.H{
			"code": 10001,
			"msg":  "商品不存在",
			"data": nil,
		})
		return
	}
	goods := model.Goods{Id: req.Id}
	if !dao.Delete(&goods) {
		c.JSON(200, gin.H{
			"code": 10002,
			"msg":  "删除失败",
			"data": nil,
		})
		return
	}
	c.JSON(200, gin.H{
		"code": 200,
		"msg":  "删除成功",
		"data": nil,
	})
}
